package es.acm.calendario.flex {
	import es.acm.calendario.DayData;
	import es.acm.calendario.IDayCalendar;
	import es.acm.calendario.IDayData;
	
	import mx.collections.ArrayCollection;
	
	import spark.components.Button;
	import spark.components.Label;
	import spark.components.SkinnableContainer;

	[SkinState("normal")]
	[SkinState("active")]
	[SkinState("todaynormal")]
	[SkinState("todayactive")]

	[SkinState("disabled")]
	[SkinState("todaydisabled")]
	
	[SkinState("over")]
	
	[Bindable]
	public class DayCalendar extends SkinnableContainer implements IDayCalendar {
		public var skinState:String;
		public var isActive:Boolean;
		public var isDisabled:Boolean;
		public var isToday:Boolean;
		public var isTodayActive:Boolean;
		public var isSelected:Boolean=false;

		[SkinPart(required="true")] 
		public var label:Label;
		
		public var events:ArrayCollection;
		public var date:Date;

		public function DayCalendar() {
			super();
			this.setStyle("skinClass", DayCalendarSkin);
			this.percentWidth=100;
			this.percentHeight=100;
			
			events = new ArrayCollection();
		}
		
		protected override function getCurrentSkinState():String {
			var returnedState:String;
			if (skinState) {
				returnedState = skinState;
			} else {
				returnedState = currentState;
			}
			return returnedState;
		}
		
		public function addEvent(event:IDayData):void {
			events.addItem(event);
			if (isToday) {
				setTodayActive();
			} else {
				setActive();
			}
		}
		
		public function setNormal():void {
			enabled = true;
			isActive = false;
			isToday = false;
			skinState = 'normal';
			useHandCursor = false;
			buttonMode = false;
			invalidateSkinState();
		}
		
		public function setActive():void {
			enabled = true;
			isActive = true;
			isToday = false;
			skinState = 'active';
			useHandCursor = true;
			buttonMode = true;
			invalidateSkinState();
		}
		
		public function setDisabled():void {
			enabled = false;
			skinState = 'disabled';
			invalidateSkinState();
		}
		
		public function setTodayNormal():void {
			enabled = true;
			isToday = true;
			skinState = 'todaynormal';
			invalidateSkinState();
		}
		
		public function setTodayActive():void {
			enabled = true;
			isToday = true;
			skinState = 'todayactive';
			invalidateSkinState();
		}
		
		public function setTodayDisabled():void {
			enabled = false;
			isToday = true;
			skinState = 'todaydisabled';
			invalidateSkinState();
		}
		
		public function setOver():void {
			skinState = 'over';
			invalidateSkinState();
		}
		
		public function setOut():void {
			if (!isToday) {
				if (isActive) {
					setActive();
				} else {
					setNormal();
				}
			} else {
				if (!isTodayActive) {
					setTodayNormal();
				} else {
					setTodayActive();
				}
			}
			invalidateSkinState();
		}
		
		public function setSelected():void {
			isSelected = true;
			invalidateSkinState();
		}
		
		public function setRestore():void {
			isSelected = false;
			invalidateSkinState();
		}
		
		override public function toString():String {
			var result:String = "- ";
			var index:int;
			for each (var event:IDayData in events) {
				result += event.title;
				index++;
				if(index < events.length) {
					result += "\n- ";
				}
			}
			return result;
		}
	}
}